package com.neusoft.neumooc.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.core.common.BaseController;
import com.neusoft.core.common.conf.Params;
import com.neusoft.core.util.MD5Util;
import com.neusoft.neumooc.service.UserService;

/**
 * @ClassName: LoginController
 * @Description: 登录控制类
 * @author lusiyu
 * @date 2016年8月17日
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController extends BaseController {

	/**
	 * @Fields BugService :登录业务逻辑处理类
	 */
	@Autowired
	UserService userService;

	/**
	 * @Title: login
	 * @Description:进入登录页面
	 * @return 登录页面
	 */
	@RequestMapping(value = "/login")
	public String login(HttpServletRequest request, HttpServletResponse response) {
		return "login";
	}

	/**
	 * @Title: getUserByName
	 * @Description:通过用户名获取用户
	 * @param request
	 *            用户名 密码
	 * @param session HttpSession
	 * @return 是否
	 */
	@ResponseBody
	@RequestMapping(value = "/getUserByName")
	public Map<String, Object> getUserByName(HttpSession session,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			// 获取参数
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			// 加密
			String passwordMD5 = MD5Util.gen(password.getBytes());
			// 用户信息
			Map<String, Object> map = userService.getUserByName(userName);
			String userId = map.get("userId").toString();
			String status = map.get("status").toString();
			String roleId = map.get("roleId").toString();
			
			// 用户名和密码是否正确
			if (map != null && map.get("password").equals(passwordMD5)) {
				// 设置session
				session.setAttribute("userId", userId);
				session.setAttribute("status", status);
				session.setAttribute("userName", userName);
				session.setAttribute("roleId", roleId);
				session.setAttribute("user", userId);
				// 封装返回值
				retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
				retMap.put(Params.RET_OBJ, "登录成功");
				return retMap;
			} else {
				// 封装返回值 用户名或密码错误
				retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				retMap.put(Params.RET_OBJ, "用户名或密码错误");
				return retMap;
			}
		} catch (Exception e) {
			log.debug("【登录】登录出现异常" + e);
			// 封装返回值 异常
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			retMap.put(Params.RET_OBJ, "登录出现异常");
			return retMap;
		}
	}

	/**
	 * @Title: logout
	 * @Description: 退出
	 * @param request
	 * @param session HttpSession
	 * @return 登录页
	 */
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		session.invalidate();
		return "login";
	}

}
